package guiClasses;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import objectClasses.Category;

public class OrganizerPanel extends GenericPanel 
{

	ArrayList<Category> categories;
	
	public OrganizerPanel(final Observer the_o_er) 
	{
		super(the_o_er);
		update(the_o_er);
	}
	
	public void update(final Observer the_o_er)
	{
		categories = Category.getCategories();
		JButton[] cat_list = new JButton[categories.size()];
		for(int i = 0; i < categories.size(); i++)
		{
			JButton temp = new JButton();
			temp.setForeground(Color.BLUE);
			final String cat_name = categories.get(i).getName();
			temp.setText(cat_name);
			temp.setBorderPainted(false);
            temp.setOpaque(false);
            temp.setBackground(new Color(139, 101, 219));
            temp.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent e) 
                {
                	setChanged();
    				notifyObservers(new CategoryPanel(the_o_er, cat_name).my_panel);
                }
            });
            temp.setCursor(new Cursor(Cursor.HAND_CURSOR));
            cat_list[i] = temp;
		}
		JPanel left = new JPanel(new GridBagLayout());
		JLabel new_cat = new JLabel("Add/Remove Category");
		left.add(new_cat, GBC.GBCGen(0,0,0,0));
		final JTextField cat_name = new JTextField(12);
		left.add(cat_name, GBC.GBCGen(0,0,0,1));
		
		JButton add_cat = new JButton("Add");
		add_cat.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	if(cat_name.getText().length()> 0)
            	{
            		if(GuiMain.getUser() != null && GuiMain.getUser().getUserType() == 'O')
                	{
		            	setChanged();
		            	Category.addCategory(new Category(cat_name.getText()));
		            	my_panel = new OrganizerPanel(the_o_er).my_panel;
						notifyObservers(my_panel);
                	}
            		else
                	{
                		String message ="You do not have permission to add categories.";
    					JOptionPane.showMessageDialog(my_panel, (Object) message);
                	}
            	}
            }
        });
		JPanel buttons = new JPanel(new GridBagLayout());
		buttons.add(add_cat, GBC.GBCGen(0,0,0,0));
		
		JButton remove_cat = new JButton("Remove");
		remove_cat.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	if(GuiMain.getUser() != null && GuiMain.getUser().getUserType() == 'O')
            	{
            		setChanged();
	            	Category.removeCategory(new Category(cat_name.getText()));
	            	my_panel = new OrganizerPanel(the_o_er).my_panel;
					notifyObservers(my_panel);
            	}
            	else
            	{
            		String message ="You do not have permission to remove categories.";
					JOptionPane.showMessageDialog(my_panel, (Object) message);
            	}
            }
        });
		buttons.add(remove_cat, GBC.GBCGen(0,0,1,0));
		left.add(buttons, GBC.GBCGen(0,0,0,2));
		
		JPanel right = new JPanel(new GridBagLayout());
		JLabel cat = new JLabel("Categories");
		JLabel space = new JLabel(" ");
		Font f = new Font(cat.getFont().getName(),Font.BOLD, 20);  
		cat.setFont(f);
		right.add(cat, GBC.GBCGen(0, 0, 0, 0));
		right.add(space, GBC.GBCGen(0, 0, 0, 1));
		
		JPanel temp = new JPanel(new GridBagLayout());
		for(int i = 0; i < cat_list.length; i++)
		{
			temp.add(cat_list[i], GBC.GBCGen(0, 0, i/6, i % 6));
		}
		JScrollPane scrollPane = new JScrollPane(temp);
		scrollPane.setPreferredSize(new Dimension(300, 190));
		right.add(scrollPane, GBC.GBCGen(0, 0, 0, 2));
		
		JButton no_cat = new JButton();
		no_cat.setForeground(Color.BLUE);
		no_cat.setText("Event Attendee List");
		no_cat.setBorderPainted(false);
        no_cat.setOpaque(false);
        no_cat.setBackground(new Color(139, 101, 219));
        no_cat.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	setChanged();	
				notifyObservers(new AttendeeListPanel(the_o_er).my_panel);
            }
        });
        no_cat.setCursor(new Cursor(Cursor.HAND_CURSOR));
		my_panel.add(left, GBC.GBCGen(0, 0, 0, 1));
		my_panel.add(right, GBC.GBCGen(0, 0, 1, 1));
		my_panel.add(no_cat, GBC.GBCGen(0, 0, 4, 1));
		my_panel.setBorder(new TitledBorder("Event Categories"));
	}

}
